package com.bookhouse.controller;

import com.bookhouse.entity.ReaderType;
import com.bookhouse.service.ReaderTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by CiusCheung on 16/4/18.
 */
@Controller
@RequestMapping("/readerType")
public class ReaderTypeController {
    @Resource(name = "readerTypeService")
    private ReaderTypeService readerTypeService;

    @RequestMapping(value = "getAllReaderType")
    public String getAllReaderType(HttpServletRequest request){
        request.setAttribute("readerTypeList",readerTypeService.findAllList(ReaderType.class));
        return "readerTypeEditList";
    }

    @RequestMapping(value = "toAddReaderType")
    public String toAddReaderType(){
        return "readerType";
    }

    @RequestMapping(value = "addReaderType")
    public String addReaderType(ReaderType readerType){
        if(readerType.getId() != null && readerType.getId()>0)
            readerTypeService.update(readerType);
        else
            readerTypeService.save(readerType);
        return "redirect:getAllReaderType";
    }

    @RequestMapping(value = "getReaderType")
    public String getReaderType(String typeId,HttpServletRequest request){
        request.setAttribute("readerType",readerTypeService.get(ReaderType.class,Integer.parseInt(typeId)));
        return "readerType";
    }

    @RequestMapping(value = "deleteReaderType")
    public String deleteReaderType(String typeId){
            readerTypeService.delete(ReaderType.class,Integer.parseInt(typeId));
        return "redirect:getAllReaderType";
    }
}
